package com.le.tester.book.concurrentjavafight.l10_1_2;

import javax.naming.InsufficientResourcesException;
import java.math.BigDecimal;

/**
 * createTime：2023/1/2 15:15
 * description：动态的锁顺序死锁，不能这么做
 */
public class TransferMoney {
    public void transferMoney(BigDecimal fromAccount, BigDecimal toAccount, BigDecimal amount) throws Exception {
        synchronized (fromAccount) {
            synchronized (toAccount) {
                if (fromAccount.compareTo(amount) < 0) {
                    throw new InsufficientResourcesException();
                } else {
                    throw new RuntimeException();
                }
            }
        }
    }

    //严格控制获取锁的顺序
    public void transferMoneyPlus(BigDecimal fromAccount, BigDecimal toAccount) {
        int fromHashCode = System.identityHashCode(fromAccount);

        int toHashCode = System.identityHashCode(toAccount);

        if (fromHashCode < toHashCode) {

        }

    }


}
